package ViolenceRecursive;

import java.util.ArrayList;
import java.util.List;

/**
 * 求一个字符串的所有子集
 *
 * @author Liaorun
 */
public class PrintAllSubsqueces {

    public static void main(String[] args) {
        PrintAllSubsqueces("123");
    }


    public static void PrintAllSubsqueces(String string) {
        char[] chars = string.toCharArray();

        process(chars, 0);
    }

    private static void process(char[] chars, int i) {
        if (i == chars.length) {
            System.out.println(chars);
            return;
        }

        // 要当前字符的路
        process(chars, i + 1);
        char tmp = chars[i];
        // 不要的字符用一个特殊字符代替
        chars[i] = 0;

        // 不要当前字符的路
        process(chars, i + 1);
        // 当前字符情况列举了，就将当前字符还原
        chars[i] = tmp;
    }

    public static void function(String str) {
        char[] chars = str.toCharArray();
        process(chars, 0, new ArrayList<>());
    }

    /**
     * @param chars      所有可以选择的字符数组
     * @param i          当前位置
     * @param characters 之前选择了的字符列表
     */
    private static void process(char[] chars, int i, List<Character> characters) {
        if (i == chars.length) {
            // 遍历了所有的字符，可以打应当前的结果
            System.out.println(characters);
            return;
        }

        List<Character> resKeep = copyList(characters);
        resKeep.add(chars[i]);

        // 要当前字符的路
        process(chars, i + 1, resKeep);
        List<Character> resNoInclude = copyList(characters);
        // 不要当前字符的路
        process(chars, i + 1, resNoInclude);
    }

    private static List<Character> copyList(List<Character> characters) {
        ArrayList<Character> list = new ArrayList<>();

        for (Character character : characters) {
            list.add(new Character(character));
        }
        return list;
    }
}
